﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace proj2
{
    public partial class imagePreview : Form
    {
        private bool dragging;

        private Point pointClicked;

        private double scale = 1.0;

        private int centerX = (Screen.AllScreens[0].Bounds.Right + Screen.AllScreens[0].Bounds.Left + 1) / 2;
        private int centerY = (Screen.AllScreens[0].Bounds.Top + Screen.AllScreens[0].Bounds.Bottom + 1) / 2;

        public imagePreview()
        {
            InitializeComponent();
        }

        public DialogResult ShowDialog(Image image)
        {
            pictureBox.Image = image;

            return this.ShowDialog();
        }

        private void imagePreview_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
            {
                System.Windows.Forms.SendKeys.Send("{ESC}");
                Close();
            }
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                if (scale == 1.0) 
                {
                    Rectangle RC = pictureBox.Bounds;
                    RC.Height = pictureBox.Image.Size.Height;
                    RC.Width = pictureBox.Image.Size.Width;
                    pictureBox.Bounds = RC;
                }

                Point pointMoveTo = new Point(pictureBox.Left + e.X, pictureBox.Top + e.Y);
                pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);

                Point loc = pictureBox.Location;

                int x = pointMoveTo.X;
                int y = pointMoveTo.Y;

                if (pictureBox.Size.Height <= this.Size.Height)
                {
                    if (y < 0)
                        y = 0;
                    pictureBox.Top = (y >= 0 && y + pictureBox.Size.Height <= this.Size.Height) ? y : ((loc.Y <= 1) ? 0 : this.Height - pictureBox.Size.Height);
                }
                else
                    pictureBox.Top = (y + pictureBox.Size.Height >= this.Size.Height && y <= 0) ? y : loc.Y;

                if (pictureBox.Size.Width <= this.Size.Width)
                {
                    if (x < 0)
                        x = 0;
                    pictureBox.Left = (x >= 0 && x + pictureBox.Size.Width <= this.Size.Width) ? x : ((loc.X <= 1) ? 0 : this.Width - pictureBox.Size.Width);
                }
                else
                    pictureBox.Left = (x + pictureBox.Size.Width >= this.Size.Width && x <= 0) ? x : loc.X;

            }
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                pointClicked = new Point(e.X, e.Y);
            }
        }

        private void pictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.SendKeys.Send("{ESC}");
            Close();
        }

        private void imagePreview_MouseWheel(object sender, MouseEventArgs e)
        {
            double oldScale = scale;
            scale = e.Delta > 0 ? scale + 0.02 : scale - 0.02;

            if (Math.Round(scale * 100) >= 20 && Math.Round(scale * 100) <= 500)
            {
                int picWidth = (int)((double)pictureBox.Image.Width * scale);
                int picHeight = (int)((double)pictureBox.Image.Height * scale);
                int x = this.Left;
                int y = pictureBox.Top;
                Rectangle RC = pictureBox.Bounds;
                RC.Height = picHeight;
                RC.Width = picWidth;
                pictureBox.Bounds = RC;
                pictureBox.Left = centerX - picWidth / 2;
                pictureBox.Top = centerY - picHeight / 2;
                pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;

            }
            else
                scale = oldScale;

        }

        private void imagePreview_Load(object sender, EventArgs e)
        {

            Rectangle RC = pictureBox.Bounds;
            RC.Height = pictureBox.Image.Size.Height;
            RC.Width = pictureBox.Image.Size.Width;
            pictureBox.Bounds = RC;

            pictureBox.Left = centerX - pictureBox.Image.Size.Width / 2;
            pictureBox.Top = centerY - pictureBox.Image.Size.Height / 2;
        }
    }
}
